<?php
header('Content-Type:application/json; charset=utf-8');
include("./api.inc.php");
require './config.php';
if ($_GET['act'] == 'activation') {
    $khd_kg = $DB->get_row("SELECT * FROM auth_pay WHERE k='khd_kg'");
    if ($khd_kg['v'] == 1) {
        $km = $_GET['code'];
        $VisitorIp = getVisitorIp();
        $row = $DB->get_row("SELECT * FROM auth_kms WHERE km = '{$km}'");
        if ($km == '') {
            $arr = array("code" => 201, "msg" => "请确保所需参数无误");
            exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
        }
        if (!$row) {
            $arr = array("code" => 201, "msg" => "卡密不存在！");
            exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
        }
        if ($row['zt'] != '待激活') {
            $daili_id = $row['daili_id']; // 获取卡密的daili_id
            // 查询这个daili_id的endtime是否小于当前时间
            $result = $DB->get_row("SELECT endtime FROM auth_user WHERE uid = $daili_id");
            //$result = $query->fetch(PDO::FETCH_ASSOC);
            if ($result['endtime'] != NULL && $result['endtime'] < time()) {
                $arr = array("code" => 201, "msg" => "代理卡密已过期");
                exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
            }
            if (time() < $row['Expiration']) {
                $DB->query("UPDATE `auth_kms` SET `VisitorIp` = '" . $VisitorIp . "',`activation` =  '" . time() . "' WHERE `auth_kms`.`id` = " . $row['id'] . ";");
                $arr = array("code" => 200, "msg" => "继续使用已激活卡密", "end_date" => date('Y-m-d H:i:s', $row['Expiration']));
                exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
            } else {
                $arr = array("code" => 201, "msg" => "卡密到期", "end_date" => date('Y-m-d H:i:s', $row['Expiration']));
                exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
            }
        }
        if ($row['type'] == 'ClassB_1') {
            if ($row['VisitorIp'] != '' && $row['VisitorIp'] != $ip) {
                $arr = array("code" => 201, "msg" => "此授权码已被使用");
                exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
            } else {
                $Expiration = strtotime("+2 hours");
            }
        } else if ($row['type'] == 'ClassB_30') {
            $Expiration = strtotime("+30 day");
        } else if ($row['type'] == 'ClassB_90') {
            $Expiration = strtotime("+90 day");
        } else if ($row['type'] == 'ClassB_365') {
            $Expiration = strtotime("+365 day");
        }
        $DB->query("UPDATE `auth_kms` SET `zt` = '已激活',`VisitorIp` = '" . $VisitorIp . "',`Expiration` = '" . $Expiration . "', `activation` = '" . time() . "' WHERE `auth_kms`.`id` = " . $row['id'] . ";");
        $arr = array("code" => 200, "msg" => "卡密激活成功", "end_date" => date('Y-m-d H:i:s', $row['Expiration']));
        exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
    } else {
        $arr = array("code" => 200, "msg" => "卡密激活成功", "end_date" => "无验证");
        exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
    }
} else {
    $arr = array("code" => 201, "msg" => "请带入请求");
    exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}
?>